package com.biblio.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.biblio.dom.Editor;
import com.biblio.exceptions.EditorAlreadyExistsException;
import com.biblio.exceptions.EditorAlreadyExistsException.IdentifierType;
import com.biblio.repository.EditorRepository;

@Transactional
@Service(value="EditorService")
@Scope("singleton")
public class EditorService {
    @Autowired   private EditorRepository editorRepository;
    public Editor checkEditor(Editor editor,String name, String web) throws EditorAlreadyExistsException {
    	
        if (editorRepository.getEditorByName(name) != null)    {
            throw new EditorAlreadyExistsException(IdentifierType.NAME, name);
        }
       
        if (editorRepository.getEditorByWeb(web) != null){
            throw new EditorAlreadyExistsException(IdentifierType.WEB, web);
        }
        
        Editor newEditor = new Editor();
        newEditor.setEditorName(name);
        newEditor.setEditorWeb(web);
        newEditor.setEditorLogo(editor.getEditorLogo());  
        editorRepository.persist(newEditor);
        return newEditor;
    }
}
